const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canCommit: false,
    phoneData: '',
    timer: '', //定时器名字
    countDownNum: '60', //倒计时初始值
    countDown: false,
    codeDisable: true,
    inputPhone: '',
    update_pwd: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    wx.getStorage({
      key: 'key',
      success: (res) => {
        self.setData({
          member_key: res.data
        });
        this.getPhoneNum();
      }
    });
    let type = options.type;
    if (type == 'update_pwd') {
      self.setData({
        update_pwd: true
      });
    }
  },
  /**
   * 获取手机号
   */
  getPhoneNum: function () {
    var self = this;
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_account&op=get_mobile_info&key=' + self.data.member_key,
      success(res) {
        self.setData({
          phoneData: res.data.datas
        });
        self.setTitle();
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //初始化图形验证码
    // this.check = this.selectComponent("#check");
    // this.check.drawPic();
  },
  /*
   *监听手机号的输入
   */
  phoneInput: function (event) {
    var self = this;
    self.setData({
      inputPhone: event.detail.value
    });
  },
  /**
   * 设置标题
   */
  setTitle: function () {
    let self = this;
    if (self.data.phoneData.state) {
      wx.setNavigationBarTitle({
        title: '手机解绑'
      })
    } else {
      wx.setNavigationBarTitle({
        title: '手机绑定'
      })
    }
  },
  /*
   *监听动态码的输入
   */
  cardInput: function (event) {
    var self = this;
    if (event.detail.value.length > 0) {
      self.data.canCommit = true;
    } else {
      self.data.canCommit = false;
    }
    self.setData({
      canCommit: self.data.canCommit,
    });
  },
  /**
   * 卡片提交
   */
  commitCard: function (event) {
    var self = this;
    if (self.data.canCommit) {
      self.commitCode(event.detail.value.cardn);
    }
  },
  // //图像验证码确认事件
  // _vertifySuccess(detail) {
  //   if (detail.detail == 1) {
  //     this.commitPhoneN();
  //   } else if (detail.detail == 2) {
  //     this.getPhoneUnbind();
  //   }
  // },
  /**
   * 提交短信验证码  下一步
   */
  commitCode: function (phoneCode) {
    var self = this;
    wx.showLoading({
      title: "加载中",
    });
    let myUrl = '';
    if (self.data.phoneData.mobile) {
      //解绑
      myUrl = app.globalData.ApiUrl + '/index.php?act=member_account&op=modify_mobile_step3';
    } else {
      //绑定
      myUrl = app.globalData.ApiUrl + '/index.php?act=member_account&op=bind_mobile_step2';
    }
    wx.request({
      url: myUrl,
      method: "POST",
      data: {
        key: self.data.member_key,
        auth_code: phoneCode
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 200) {
          let pages = getCurrentPages();
          let prevPage = pages[pages.length - 2];
          prevPage.setData({
            bind_phone_back: true,
          });
          if (self.data.update_pwd) {
            wx.navigateTo({
              'url': '../member_update_pwd/update_pwd'
            })
          } else {
            wx.navigateBack({
              delta: 1
            });
          }

          if (self.data.phoneData.state) {
            wx.showToast({
              title: '手机号解绑成功',
              icon: 'none'
            });
          } else {
            wx.showToast({
              title: '手机号绑定成功',
              icon: 'none'
            });
          }

        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    })
  },
  /**
   * 获取短信验证码1
   */
  getPhoneCode: function () {
    let self = this;
    if (self.data.phoneData.state) {
      // self.check.drawPic();
      // self.check.showDialog(2);
      this.getPhoneUnbind();
    } else {
      if (self.data.inputPhone) {
        // self.check.drawPic();
        // self.check.showDialog(1);
        this.commitPhoneN();
      } else {
        wx.showToast({
          title: '请输入正确的手机号!',
          icon: 'none'
        })
      }
    }
  },
  /**
   * 解绑获取手机短信验证码
   */
  getPhoneUnbind: function () {
    let self = this;
    wx.showLoading({
      title: "加载中",
    })
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_account&op=modify_mobile_step2',
      method: "POST",
      data: {
        key: self.data.member_key,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(app.globalData.ApiUrl + '/index.php?/act=member_account&op=modify_mobile_step2');
        if (res.data.code == 200) {
          self.data.countDownNum = res.data.datas.sms_time;
          self.countDown();
          self.setData({
            codeDisable: false
          });
          wx.showToast({
            title: '验证码已发送!',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    });
  },
  /**
   * 根据输入的手机号获取短信验证码
   */
  commitPhoneN: function () {
    let self = this;
    wx.showLoading({
      title: "加载中",
    })
    wx.request({
      url: app.globalData.ApiUrl + '/index.php?act=member_account&op=bind_mobile_step1',
      method: "POST",
      data: {
        key: self.data.member_key,
        mobile: self.data.inputPhone
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.data.code == 200) {
          self.data.countDownNum = res.data.datas.sms_time;
          self.countDown();
          self.setData({
            codeDisable: false
          });
          wx.showToast({
            title: '验证码已发送!',
            icon: 'success'
          })
        } else {
          wx.showToast({
            title: res.data.datas.error,
            icon: 'none'
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络请求失败',
          icon: 'none'
        })
      }
    });
  },
  /**
   * 验证码倒计时
   */
  countDown: function () {
    let that = this;
    that.setData({
      countDown: true
    })
    let countDownNum = that.data.countDownNum; //获取倒计时初始值
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum == 0) {
          //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
          //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理codes go here
          that.setData({
            countDown: false
          })
        }
      }, 1000)
    })
  }
})